package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dto.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validator.ValidatorAdd;
import com.heima.model.common.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "频道管理API接口",tags = "频道管理API接口",description = "管理频道相关接口")
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {
    @Autowired
    private AdChannelService adChannelService;

    /*
    * todo 频道列表分页查询
    *  */
    @ApiOperation(value = "分页查询频道列表",notes = "按照name模糊查询，分页查询 按照ord升序排序")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO channelDTO){
        return this.adChannelService.findByNameAndPage(channelDTO);
    }
    /*
    * todo 频道新增
    *   */
    @ApiOperation("频道新增")
    @PostMapping("/save")
    public ResponseResult addAdChannel(@RequestBody @Validated(ValidatorAdd.class) AdChannel adChannel){
        return this.adChannelService.addAdChannel(adChannel);
    }
    /*
    * TODO 频道修改
    *  */
    @ApiOperation("频道修改")
    @PostMapping("/update")
    public ResponseResult updateChannel(@RequestBody @Validated(ValidatorUpdate.class) AdChannel adChannel){
        return this.adChannelService.updateChannel(adChannel);
    }

    /*
    * TODO 频道删除
    *  */
    @ApiOperation("频道删除")
    @GetMapping("del/{id}")
    public ResponseResult deleteById(@PathVariable("id")Integer id){
       return this.adChannelService.deleteById(id);
    }


    /*
     * TODO 频道列表展示---查询所有频道的数据
     *  */
    @ApiOperation("查询所有频道的数据")
    @GetMapping("/channels")
    public ResponseResult findAll(){
        List<AdChannel> list = adChannelService.list();
        return ResponseResult.okResult(list);
    }

    /**
     * 根据id查询频道
     * @param id
     * @return
     */
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(adChannelService.getById(id));
    }

}
